package com.bio.jsf.util;

import java.io.Serializable;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.bio.jpa.entities.User;
import org.primefaces.event.FlowEvent;

@ManagedBean(name = "userWizard")
@RequestScoped
public class UserWizard implements Serializable {

    private User user = new User();
    private boolean skip;
    private static final Logger logger = Logger.getLogger(UserWizard.class.getName());

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void save(ActionEvent actionEvent) {
        //Persist user  

        FacesMessage msg = new FacesMessage("Successful", "Welcome :" + user.getUserLogin());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public boolean isSkip() {
        return skip;
    }

    public void setSkip(boolean skip) {
        this.skip = skip;
    }

    public String onFlowProcess(FlowEvent event) {
        //First perform check to see if the user is attempting to remove the last visitor
        System.out.println("fdfsdfsd"+event.getNewStep());
        if ("confirm".equals(event.getNewStep())) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Visit must have at least one visitor.", null);
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return event.getOldStep();
        }

        return event.getNewStep();
    }
}
